<?php

namespace App\Service;

use App\Model\ActiveJoinModel;
use App\Model\ActiveModel;
use App\Model\AdjacentKitchenModel;
use App\Model\AdjacentOlderModel;
use App\Model\AdjacentSchoolModel;
use App\Model\AdjacentToolModel;
use App\Model\AssetsInOutModel;
use App\Model\AssetsModel;
use App\Model\AssetsTypeModel;
use App\Model\CommunityTopicModel;
use App\Model\ConfigPatrolItemModel;
use App\Model\ConfigPatrolModel;
use App\Model\ConfigRepairItemModel;
use App\Model\ConfigRepairModel;
use App\Model\ExamineAdjacentKitchenModel;
use App\Model\ExamineAdjacentOlderModel;
use App\Model\ExamineAdjacentSchoolModel;
use App\Model\ExamineAdjacentToolsModel;
use App\Model\ExaminePatrolModel;
use App\Model\ExamineRepairModel;
use App\Model\ExamineVisitModel;
use App\Model\LogWorkModel;
use App\Model\NoticeModel;
use App\Model\OrderModel;
use App\Model\PropertyRepairModel;
use App\Model\PropertyVisitModel;
use App\Model\ReportModel;
use App\Model\ShareLatticeGoodsModel;
use App\Model\ShareLatticeOrderModel;
use App\Model\StaffModel;
use App\Model\StaffNavModel;
use App\Model\StaffTypeModel;
use App\Model\UserCarModel;
use App\Model\UserHouseModel;
use App\Model\UserModel;
use App\Utility\DataConversion;
use App\Utility\JwtManager;
use App\Utility\Password;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

class StaffService extends BaseService
{
    /**
     * @param $username
     * @param $password
     * @param $ip
     * @return array
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    function StaffLogin($username, $password, $ip): array
    {
        $user = StaffModel::create()->getUser(['username' => $username]);
        if (!$user) {
            $user = StaffModel::create()->getUser(['phone' => $username]);
            if (!$user) {
                return [
                    'result' => false,
                    'msg' => '账户不存在'
                ];
            }
        }
        if (!Password::getInstance()->validate($password, $user->password)) {
            return [
                'result' => false,
                'msg' => '账户密码不匹配'
            ];
        }
        $res = StaffModel::create()->update(['login_time' => time(), 'login_ip' => $ip], ['id' => $user->id]);
        $user->password = '';

        $userJwt = JwtManager::getInstance()->encodeStaff($user->id, $user->phone, $user->username, $user->type);
        $users = $user->toArray();
        $data = $users;
        $data['token'] = $userJwt;
        unset($data['password']);
        return [
            'result' => true,
            'msg' => '登陆成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getStaffNav($sid, $type): array
    {
        $auth = StaffTypeModel::create()->getShop(['id' => $type]);
        if (!$auth) {
            return [
                'result' => false,
                'msg' => '当前账户权限不足'
            ];
        }

        $au = explode(',', $auth->rule);
        $navs = StaffNavModel::create()->where("id", $au, 'IN')->order("sort", "desc")->all();
        $data = [
            'name' => $auth->name,
            'desc' => $auth->desc,
            'nav' => []
        ];

        foreach ($navs as $nav) {
            $data['nav'][] = [
                'id' => $nav['id'],
                'method' => $nav['method'],
                'name' => $nav['name'],
                'icon' => $nav['icon'],
                'image' => $nav['image'],
                'desc' => $nav['description']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $password
     * @param $new_password
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function changePass($sid, $password, $new_password): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '该用户不存在或已注销'
            ];
        }

        if (!Password::getInstance()->validate($password, $staff->password)) {
            return [
                'result' => false,
                'msg' => '账户密码不匹配'
            ];
        }

        $newP = Password::getInstance()->encryption($new_password);
        $res = StaffModel::create()->update(['password' => $newP], ['id' => $sid]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '修改成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $status
     * @param $limit
     * @param $page
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getVisit($status, $limit, $page): array
    {
        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }

        $model = PropertyVisitModel::create()->where($where);

        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $list = $model->order('id', 'DESC')->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();

        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $visit) {
            $data[] = [
                'id' => $visit['id'],
                'uid' => $visit['uid'],
                'house' => $visit['house'],
                'time' => date("Y-m-d H:i", $visit['time']),
                'name' => $visit['name'],
                'tel' => DataConversion::getInstance()->hidtel($visit['tel']),
                'person' => $visit['person'],
                'is_car' => $visit['is_car'],
                'car' => $visit['car'],
                'status' => $visit['status'],
                'status_label' => ConfigService::getInstance()->getVisitStatus($visit['status'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getVisitDetail($id): array
    {
        $visit = PropertyVisitModel::create()->getProperty(['id' => $id]);
        if (!$visit) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }

        $visit = $visit->toArray();

        $data = [
            'id' => $visit['id'],
            'uid' => $visit['uid'],
            'house' => $visit['house'],
            'time' => date("Y-m-d H:i", $visit['time']),
            'name' => $visit['name'],
            'tel' => $visit['tel'],
            'person' => $visit['person'],
            'is_car' => $visit['is_car'],
            'car' => $visit['car'],
            'status' => $visit['status'],
            'status_label' => ConfigService::getInstance()->getVisitStatus($visit['status']),
            'create_time' => date("Y-m-d H:i:s", $visit['create_time'])
        ];


        $info = ConfigService::getInstance()->getUserInfo($data['uid']);
        $data = array_merge($data, $info);
        $examine = ExamineVisitModel::create()->getLog(['pid' => $id]);
        $data['memo'] = $examine->memo ?? '-';
        $data['examine_time'] = isset($examine->create_time) ? date("Y-m-d H:i:s", $examine->create_time) : '-';
        $data['phone'] = $data['u_tel'];
        $data['tel'] = DataConversion::getInstance()->hidtel($data['tel']);
        $data['u_tel'] = DataConversion::getInstance()->hidtel($data['u_tel']);
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function searchVisit($keyword): array
    {
        $visits = PropertyVisitModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($visits)) {
            $visits = PropertyVisitModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($visits)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($visits as $visit) {
            $data[] = [
                'id' => $visit['id'],
                'uid' => $visit['uid'],
                'house' => $visit['house'],
                'time' => date("Y-m-d H:i", $visit['time']),
                'name' => $visit['name'],
                'tel' => DataConversion::getInstance()->hidtel($visit['tel']),
                'person' => $visit['person'],
                'is_car' => $visit['is_car'],
                'car' => $visit['car'],
                'status' => $visit['status'],
                'status_label' => ConfigService::getInstance()->getVisitStatus($visit['status'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $status
     * @param $memo
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     * @throws Exception
     */
    function examineVisit($sid, $id, $status, $memo): array
    {
        $visit = PropertyVisitModel::create()->getProperty(['id' => $id, "status" => 0]);
        if (!$visit) {
            return [
                'result' => false,
                'msg' => '数据获取失败'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'uid' => $visit->uid,
            'sid' => $sid,
            'pid' => $id,
            'memo' => $memo,
            'status' => $status,
            'type' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];

        $res = ExamineVisitModel::create()->addLog($par);
        if ($res) {
            $r = PropertyVisitModel::create()->update(['status' => $status], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $start
     * @param $end
     * @param $status
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getMyVisit($sid, $start, $end, $status): array
    {
        $start = strtotime($start);
        $end = strtotime($end);
        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }
        $examine = ExamineVisitModel::create()->where(['sid' => $sid])->where("create_time", $start, ">=")->where("create_time", $end, "<")->all($where);
        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $ids = array_column($examine, 'pid');

        $visits = PropertyVisitModel::create()->where('id', $ids, 'IN')->all(['is_deleted' => 0]);

        $data = [];
        foreach ($visits as $visit) {
            $data[] = [
                'id' => $visit['id'],
                'uid' => $visit['uid'],
                'house' => $visit['house'],
                'time' => date("Y-m-d H:i", $visit['time']),
                'name' => $visit['name'],
                'tel' => DataConversion::getInstance()->hidtel($visit['tel']),
                'person' => $visit['person'],
                'is_car' => $visit['is_car'],
                'car' => $visit['car'],
                'status' => $visit['status'],
                'status_label' => ConfigService::getInstance()->getVisitStatus($visit['status'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $status
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getRepair($status, $limit, $page): array
    {
        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }

        $model = PropertyRepairModel::create()->where($where);

        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $repairs = $model->order('id', 'DESC')->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();

        if (empty($repairs)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($repairs as $repair) {
            $data[] = [
                'id' => $repair['id'],
                'uid' => $repair['uid'],
                'house' => $repair['house'],
                'time' => date("Y-m-d H:i", $repair['time']),
                'name' => $repair['name'],
                'tel' => $repair['tel'],
                'name1' => $repair['name1'],
                'tel1' => $repair['tel1'],
                'content' => $repair['content'],
                'images' => json_decode($repair['images'], true),
                'type' => ConfigService::getInstance()->getRepairType($repair['type']),
                'status' => $repair['status'],
                'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
                'code' => $repair['create_time']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getRepairDetail($id): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $id]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }

        $rType = ConfigRepairModel::create()->getConfig(['id' => $repair->type]);

        $repair = $repair->toArray();

        $data = [
            'id' => $repair['id'],
            'uid' => $repair['uid'],
            'house' => $repair['house'],
            'time' => date("Y-m-d H:i", $repair['time']),
            'name' => $repair['name'],
            'tel' => $repair['tel'],
            'name1' => $repair['name1'],
            'tel1' => $repair['tel1'],
            'content' => $repair['content'],
            'images' => json_decode($repair['images'], true),
            'type' => ConfigService::getInstance()->getRepairType($repair['type']),
            'rType' => $rType,
            'status' => $repair['status'],
            'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
            'code' => $repair['create_time'],
            "create_time" => date("Y-m-d H:i:s", $repair['create_time'])
        ];


        $examine = ExamineRepairModel::create()->getLog(['pid' => $id]);
        if (!$examine) {
            $examine = [];
        } else {
            $examine = $examine->toArray();
            $info = ConfigService::getInstance()->getStaffInfo($examine['sid']);
            $data = array_merge($data, $info);
            $examine['examine_time'] = date("Y-m-d H:i:s", $examine['create_time']);
            $examine['examine_new_time'] = date("Y-m-d H:i:s", $examine['update_time']);
            $examine['offer_image'] = json_decode($examine['offer_image'], true);
            $examine['complete_image'] = json_decode($examine['complete_image'], true);
            unset($examine['update_time']);
            unset($examine['create_time']);
            unset($examine['code']);
        }
        $data = array_merge($data, ['examine' => $examine]);
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function searchRepair($keyword): array
    {
        $repairs = PropertyRepairModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($repairs)) {
            $repairs = PropertyRepairModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($repairs)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($repairs as $repair) {
            $data[] = [
                'id' => $repair['id'],
                'uid' => $repair['uid'],
                'house' => $repair['house'],
                'time' => date("Y-m-d H:i", $repair['time']),
                'name' => $repair['name'],
                'tel' => $repair['tel'],
                'name1' => $repair['name1'],
                'tel1' => $repair['tel1'],
                'content' => $repair['content'],
                'images' => json_decode($repair['images'], true),
                'type' => ConfigService::getInstance()->getRepairType($repair['type']),
                'status' => $repair['status'],
                'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
                'code' => $repair['create_time']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function grabbingRepair($sid, $id): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $id, 'status' => 0]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '数据获取失败'
            ];
        }
        $config = ConfigRepairModel::create()->getConfig(['id' => $repair->type]);
        $engineer = StaffModel::create()->getUser(['id' => $sid]);
        $par = [
            'uid' => $repair->uid,
            'sid' => $sid,
            'pid' => $id,
            'engineer' => $engineer->name ?? '物业工程师'
        ];

        if ($config->type == 1) {
            $par['status'] = 3;
        }

        $res = ExamineRepairModel::create()->addLog($par);
        $res1 = PropertyRepairModel::create()->update(['status' => 2], ['id' => $id]);
        if (!$res || !$res1) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '抢单成功',
            'data' => 'success'
        ];

    }

    /**
     * @param $sid
     * @param $id
     * @param $image
     * @param $price
     * @param $memo
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function offerRepair($sid, $id, $image, $price, $memo): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $id, 'status' => 2]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '数据获取失败'
            ];
        }
        $examine = ExamineRepairModel::create()->getLog(['pid' => $id, 'sid' => $sid, 'status' => 0]);
        if (!$examine) {
            return [
                'result' => false,
                'msg' => '任务暂未分配'
            ];
        }


        $par = [
            'offer_image' => json_encode($image),
            'offer_memo' => $memo,
            'offer_price' => $price,
            'status' => 2
        ];

        $res = ExamineRepairModel::create()->update($par, ['pid' => $id, 'sid' => $sid]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        if ($repair->sid == 0) {
            return [
                'result' => true,
                'msg' => '报价成功',
                'data' => 'success'
            ];
        } else {
            return [
                'result' => true,
                'msg' => '报价成功',
                'data' => $repair->id
            ];
        }

    }

    /**
     * @param $sid
     * @param $id
     * @param $image
     * @param $code
     * @param $memo
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function completeRepair($sid, $id, $image, $code, $memo): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $id, 'status' => 2]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '数据获取失败'
            ];
        }
        $rT = ConfigRepairModel::create()->getConfig(['id' => $repair->type]);
        if ($rT->type == 1) {
            $examine = ExamineRepairModel::create()->getLog(['pid' => $id, 'sid' => $sid, 'status' => 3]);
        } else {
            $examine = ExamineRepairModel::create()->getLog(['pid' => $id, 'sid' => $sid, 'status' => 3, 'code' => $code]);
        }
        if (!$examine) {
            return [
                'result' => false,
                'msg' => '任务不存在或识别码错误'
            ];
        }

        $time = date("Y-m-d");
        $c_time = date("Y-m-d", $examine->create_time);
        $par = [
            'complete_image' => json_encode($image),
            'complete_memo' => $memo,
            'status' => 1,
            'is_complete' => $time == $c_time ? 1 : 0
        ];

        $res = ExamineRepairModel::create()->update($par, ['pid' => $id, 'sid' => $sid]);

        $res1 = PropertyRepairModel::create()->update(['status' => 1], ['id' => $id]);
        if (!$res || !$res1) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '提交成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $un_complete_memo
     * @param $is_complete
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function unCompleteRepair($sid, $id, $un_complete_memo, $is_complete): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $id, 'status' => 2]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '数据获取失败'
            ];
        }
        $examine = ExamineRepairModel::create()->getLog(['pid' => $id, 'sid' => $sid, 'status' => 3]);
        if (!$examine) {
            return [
                'result' => false,
                'msg' => '任务不存在'
            ];
        }

        $par = [
            'un_complete_memo' => $un_complete_memo,
            'is_complete' => $is_complete
        ];

        $res = ExamineRepairModel::create()->update($par, ['pid' => $id, 'sid' => $sid]);

        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '提交成功',
            'data' => 'success'
        ];

    }

    /**
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function searchTypeRepair($type): array
    {
        $repairs = PropertyRepairModel::create()->where(['type' => $type])->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($repairs)) {

            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($repairs as $repair) {
            $data[] = [
                'id' => $repair['id'],
                'uid' => $repair['uid'],
                'house' => $repair['house'],
                'time' => date("Y-m-d H:i", $repair['time']),
                'name' => $repair['name'],
                'tel' => $repair['tel'],
                'name1' => $repair['name1'],
                'tel1' => $repair['tel1'],
                'content' => $repair['content'],
                'images' => json_decode($repair['images'], true),
                'type' => ConfigService::getInstance()->getRepairType($repair['type']),
                'status' => $repair['status'],
                'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
                'code' => $repair['create_time']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $start
     * @param $end
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getMyRepair($sid, $start, $end, $status): array
    {
        $start = strtotime($start);
        $end = strtotime($end);
        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }
        $examine = ExamineRepairModel::create()->where(['sid' => $sid])->where("create_time", $start, ">=")->where("create_time", $end, "<")->all($where);
        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $ids = array_column($examine, 'pid');

        $repairs = PropertyRepairModel::create()->where('id', $ids, 'IN')->all(['is_deleted' => 0]);

        $data = [];
        foreach ($repairs as $repair) {
            $data[] = [
                'id' => $repair['id'],
                'uid' => $repair['uid'],
                'house' => $repair['house'],
                'time' => date("Y-m-d H:i", $repair['time']),
                'name' => $repair['name'],
                'tel' => $repair['tel'],
                'name1' => $repair['name1'],
                'tel1' => $repair['tel1'],
                'content' => $repair['content'],
                'images' => json_decode($repair['images'], true),
                'type' => ConfigService::getInstance()->getRepairType($repair['type']),
                'status' => $repair['status'],
                'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
                'code' => $repair['create_time']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getPatrolStatic($sid): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '账户失效'
            ];
        }

        $auth = StaffTypeModel::create()->getShop(['id' => $staff->type]);
        if (!$auth) {
            return [
                'result' => false,
                'msg' => '权限不足'
            ];
        }
        $patrol = ConfigPatrolModel::create()->where("rule", "%" . $auth->name . '%', 'LIKE')->all(['is_deleted' => 0]);
        if (empty($patrol)) {
            return [
                'result' => false,
                'msg' => '暂无设备'
            ];
        }
        $ids = array_column($patrol, 'code');
        $where = ['is_deleted' => 0];
        $start = strtotime(date("Y-m-d"));
        $end = strtotime(date("Y-m-d 23:59:59"));
        $suc = ExaminePatrolModel::create()
            ->where(['sid' => $sid])
            ->where("code", $ids, 'IN')
            ->where("create_time", $start, '>')
            ->where("create_time", $end, '<')
            ->where(['is_deleted' => 0])
            ->where(['status' => 1])
            ->group("code")
            ->all();
        $fai = ExaminePatrolModel::create()
            ->where(['sid' => $sid])
            ->where("code", $ids, 'IN')
            ->where("create_time", $start, '>')
            ->where("create_time", $end, '<')
            ->where(['is_deleted' => 0])
            ->where(['status' => -1])
            ->group("code")
            ->all();

        $total = 0;
        foreach ($patrol as $item) {
            if ($item['type'] == 0) {
                $total += $item['num'];
            }
        }

        $date = [
            'total' => $total,
            'complete' => count($suc) + count($fai),
            'incomplete' => $total - count($suc) - count($fai),
            'normal' => count($suc),
            'abnormal' => count($fai),
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $date
        ];
    }

    /**
     * @param $sid
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getPatrolList($sid, $status): array
    {
        $start = strtotime(date("Y-m-d"));
        $end = strtotime(date("Y-m-d 23:59:59"));
        $examine = ExaminePatrolModel::create()->where("create_time", $start, '>')->where("create_time", $end, '<')->where(['is_deleted' => 0])->field("count(id) as num, code, create_time,status, author")->group("code")->all();
        $eid = array_column($examine, 'code');
        $times = array_column($examine, 'create_time', 'code');
        $statuses = array_column($examine, 'status', 'code');
        $exa_count = array_column($examine, 'num', 'code');
        $exa_author = array_column($examine, 'author', 'code');
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '账户失效'
            ];
        }
        $auth = StaffTypeModel::create()->getShop(['id' => $staff->type]);
        if (!$auth) {
            return [
                'result' => false,
                'msg' => '权限不足'
            ];
        }

        $patrol = ConfigPatrolModel::create()->where("rule", "%" . $auth->name . '%', 'LIKE')->all(['is_deleted' => 0]);
        if (empty($patrol)) {
            return [
                'result' => false,
                'msg' => '暂无设备'
            ];
        }
        $data = [];
        foreach ($patrol as $value) {
            $r = [
                'id' => $value['id'],
                'name' => $value['name'],
                'code' => $value['code'],
                'address' => $value['address'],
                'image' => $value['image'],
                'desc' => $value['desc'],
                'time' => date("Y-m-d H:i:s", $times[$value['code']] ?? 0),
                'status' => $statuses[$value['code']] ?? 0,
                'total' => $value['num'],
                'num' => $exa_count[$value['code']] ?? 0,
                'type' => $value['type'],
                'author' => $exa_author[$value['code']] ?? '-'
            ];
            if ($status == 99) {
                if (in_array($value['code'], $eid)) {
                    $r['complete'] = 1;
                } else {
                    $r['complete'] = 0;
                }
                $data[] = $r;
            } elseif ($status == 1) {
                if (in_array($value['code'], $eid)) {
                    $r['complete'] = 1;
                    $data[] = $r;
                }
            } else {
                if (!in_array($value['code'], $eid)) {
                    $r['complete'] = 0;
                    $data[] = $r;
                }
            }
        }
        if (empty($data)) {
            return [
                'return' => false,
                'msg' => '暂无数据'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $status
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getPatrolStatusList($sid, $status): array
    {
        $where = ['is_deleted' => 0];
        if ($status == 1) {
            $where['status'] = 1;
        } else {
            $where['status'] = -1;
        }
        $start = strtotime(date("Y-m-d"));
        $end = strtotime(date("Y-m-d 23:59:59"));
        $examine = ExaminePatrolModel::create()->where(['sid' => $sid])->where("create_time", $start, '>')->where("create_time", $end, '<')->where($where)->all();

        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $eid = array_column($examine, 'code');
        $times = array_column($examine, 'create_time', 'code');

        $patrol = ConfigPatrolModel::create()->where("code", $eid, 'IN')->all();

        $data = [];
        foreach ($patrol as $value) {
            $num = 0;
            foreach ($examine as $e) {
                if ($e['code'] == $value['code']) {
                    $num++;
                }
            }
            $data[] = [
                'id' => $value['id'],
                'name' => $value['name'],
                'code' => $value['code'],
                'address' => $value['address'],
                'image' => $value['image'],
                'desc' => $value['desc'],
                'type' => $value['type'],
                'complete' => 1,
                'status' => $status,
                'time' => date("Y-m-d H:i:s", $times[$value['code']]),
                'num' => $num,
                'total' => $value['num']
            ];
        }

        if (empty($data)) {
            return [
                'return' => false,
                'msg' => '暂无数据'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $code
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getPatrolDetail($code): array
    {
        $patrol = ConfigPatrolModel::create()->getConfig(['code' => $code]);
        if (!$patrol) {
            return [
                'result' => false,
                'msg' => '该设备已删除'
            ];
        }

        $start = strtotime(date("Y-m-d"));
        $end = strtotime(date("Y-m-d 23:59:59"));
        $examine = ExaminePatrolModel::create()->where("create_time", $start, '>')->where("create_time", $end, '<')->all(['code' => $patrol->code]);
        $patrol = $patrol->toArray();

        $par = [
            'id' => $patrol['id'],
            'name' => $patrol['name'],
            'code' => $patrol['code'],
            'address' => $patrol['address'],
            'image' => $patrol['image'],
            'desc' => $patrol['desc'],
            'total' => $patrol['num'],
            'type' => $patrol['type']
        ];
        $checkList = explode(',', $patrol['item']);
        $item = [];
        if (!empty($checkList)) {
            $item = ConfigPatrolItemModel::create()->where("name", $checkList, "IN")->all();
        }
        $checkData = [];
        foreach ($item as $i) {
            $checkData[] = [
                'name' => $i['name'],
                'type' => $i['type'],
                'select' => explode('|', $i['description'])
            ];
        }

        $par['content'] = $checkData;

        if (empty($examine)) {
            $par['examine'][]['complete'] = 0;
            $par['num'] = 0;
        } else {
            $par['num'] = count($examine);
            foreach ($examine as $v) {
                $par['examine'][] = [
                    'complete' => 1,
                    'status' => $v['status'],
                    'time' => date("Y-m-d H:i:s", $v['create_time']),
                    'author' => $v['author'],
                    'images' => json_decode($v['image'], true),
                    'memo' => $v['memo'],
                    'content' => json_decode($v['content'], true)
                ];
            }
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $par
        ];
    }

    /**
     * @param $sid
     * @param $code
     * @param $memo
     * @param $status
     * @param $image
     * @param $content
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function examinePatrol($sid, $code, $memo, $status, $image, $content): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        $par = [
            'sid' => $sid,
            'code' => $code,
            'memo' => $memo,
            'status' => $status,
            'author' => $staff->name ?? $staff->nickname,
            'content' => $content,
            'image' => json_encode($image)
        ];

        $res = ExaminePatrolModel::create()->addLog($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $status
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentSchool($status, $limit, $page): array
    {
        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }
        $model = AdjacentSchoolModel::create()->where($where);

        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $list = $model->order('id', 'DESC')->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();

        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($list as $item) {
            $share = ConfigService::getInstance()->getServiceItem($item['nid']);
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'house' => $item['house'],
                'child_name' => $item['child_name'],
                'child_grade' => $item['child_grade'],
                'child_hobby' => $item['child_hobby'],
                'child_age' => $item['child_age'],
                'name1' => $item['name1'],
                'tel1' => $item['tel1'],
                'title' => $share->name,
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getShareStatus($item['status']),
                'memo' => $share->label,
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'during' => DataConversion::getInstance()->getDays($item['start_time'], $item['end_time']),
                'fee' => $share->price
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentSchoolDetail($id): array
    {
        $school = AdjacentSchoolModel::create()->getAdjacent(['id' => $id]);
        if (!$school) {
            return [
                'result' => false,
                'msg' => '该订单已删除'
            ];
        }

        $school = $school->toArray();
        $share = ConfigService::getInstance()->getServiceItem($school['nid']);
        $data = [
            'id' => $school['id'],
            'nid' => $school['nid'],
            'name' => $school['name'],
            'tel' => $school['tel'],
            'house' => $school['house'],
            'child_name' => $school['child_name'],
            'child_grade' => $school['child_grade'],
            'child_hobby' => $school['child_hobby'],
            'child_age' => $school['child_age'],
            'name1' => $school['name1'],
            'tel1' => $school['tel1'],
            'title' => $share->name,
            'status' => $school['status'],
            'status_label' => ConfigService::getInstance()->getShareStatus($school['status']),
            'memo' => $share->label,
            'time' => date("Y-m-d", $school['start_time']) . ' ~ ' . date('Y-m-d', $school['end_time']),
            'during' => DataConversion::getInstance()->getDays($school['start_time'], $school['end_time']),
            'fee' => $share->price
        ];

        $examine = ExamineAdjacentSchoolModel::create()->getLog(['pid' => $school['id']]);
        $data['examine'] = $examine ? $examine->toArray() : [];

        return [
            'result' => true,
            'msg' => '查询成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $status
     * @param $memo
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function examineAdjacentSchool($sid, $id, $status, $memo): array
    {
        $school = AdjacentSchoolModel::create()->getAdjacent(['id' => $id, 'status' => 0]);
        if (!$school) {
            return [
                'result' => false,
                'msg' => '该订单已删除'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'uid' => $school->uid,
            'sid' => $sid,
            'pid' => $id,
            'memo' => $memo,
            'status' => $status,
            'type' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];

        $res = ExamineAdjacentSchoolModel::create()->addLog($par);
        if ($res) {
            if ($status == 1) {
                $s = 2;
            } else {
                $s = -1;
            }
            $r = AdjacentSchoolModel::create()->update(['status' => $s], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyAdjacentSchool($sid): array
    {
        $examine = ExamineAdjacentSchoolModel::create()->where(['is_deleted' => 0, 'sid' => $sid])->all();
        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $eid = array_column($examine, 'pid');
        $school = AdjacentSchoolModel::create()->where("id", $eid, "IN")->all(['is_deleted' => 0]);
        if (empty($school)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($school as $item) {
            $share = ConfigService::getInstance()->getServiceItem($item['nid']);
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'house' => $item['house'],
                'child_name' => $item['child_name'],
                'child_grade' => $item['child_grade'],
                'child_hobby' => $item['child_hobby'],
                'child_age' => $item['child_age'],
                'name1' => $item['name1'],
                'tel1' => $item['tel1'],
                'title' => $share->name,
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getShareStatus($item['status']),
                'memo' => $share->label,
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'during' => DataConversion::getInstance()->getDays($item['start_time'], $item['end_time']),
                'fee' => $share->price
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchAdjacentSchool($keyword): array
    {
        $school = AdjacentSchoolModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($school)) {
            $school = AdjacentSchoolModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($visits)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($school as $item) {
            $share = ConfigService::getInstance()->getServiceItem($item['nid']);
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'house' => $item['house'],
                'child_name' => $item['child_name'],
                'child_grade' => $item['child_grade'],
                'child_hobby' => $item['child_hobby'],
                'child_age' => $item['child_age'],
                'name1' => $item['name1'],
                'tel1' => $item['tel1'],
                'title' => $share->name,
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getShareStatus($item['status']),
                'memo' => $share->label,
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'during' => DataConversion::getInstance()->getDays($item['start_time'], $item['end_time']),
                'fee' => $share->price
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $status
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentOlder($status, $limit, $page): array
    {
        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }
        $model = AdjacentOlderModel::create()->where($where);

        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $list = $model->order('id', 'DESC')->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();

        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($list as $item) {
            $share = ConfigService::getInstance()->getServiceItem($item['nid']);
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'house' => $item['house'],
                'older_name' => $item['older_name'],
                'older_age' => $item['older_age'],
                'older_sex' => $item['older_sex'],
                'title' => $share->name,
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getShareStatus($item['status']),
                'memo' => $share->label,
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'during' => DataConversion::getInstance()->getDays($item['start_time'], $item['end_time']),
                'fee' => $share->price
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentOlderDetail($id): array
    {
        $school = AdjacentOlderModel::create()->getAdjacent(['id' => $id]);
        if (!$school) {
            return [
                'result' => false,
                'msg' => '该订单已删除'
            ];
        }

        $school = $school->toArray();
        $share = ConfigService::getInstance()->getServiceItem($school['nid']);
        $data = [
            'id' => $school['id'],
            'nid' => $school['nid'],
            'name' => $school['name'],
            'tel' => $school['tel'],
            'house' => $school['house'],
            'older_name' => $school['older_name'],
            'older_age' => $school['older_age'],
            'older_sex' => $school['older_sex'],
            'title' => $share->name,
            'status' => $school['status'],
            'status_label' => ConfigService::getInstance()->getShareStatus($school['status']),
            'memo' => $share->label,
            'time' => date("Y-m-d", $school['start_time']) . ' ~ ' . date('Y-m-d', $school['end_time']),
            'during' => DataConversion::getInstance()->getDays($school['start_time'], $school['end_time']),
            'fee' => $share->price
        ];

        $examine = ExamineAdjacentOlderModel::create()->getLog(['pid' => $school['id']]);
        $data['examine'] = $examine ? $examine->toArray() : [];

        return [
            'result' => true,
            'msg' => '查询成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $status
     * @param $memo
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function examineAdjacentOlder($sid, $id, $status, $memo): array
    {
        $school = AdjacentOlderModel::create()->getAdjacent(['id' => $id, 'status' => 0]);
        if (!$school) {
            return [
                'result' => false,
                'msg' => '该订单已删除'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'uid' => $school->uid,
            'sid' => $sid,
            'pid' => $id,
            'memo' => $memo,
            'status' => $status,
            'type' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];

        $res = ExamineAdjacentOlderModel::create()->addLog($par);
        if ($res) {
            if ($status == 1) {
                $s = 2;
            } else {
                $s = -1;
            }
            $r = AdjacentOlderModel::create()->update(['status' => $s], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyAdjacentOlder($sid): array
    {
        $examine = ExamineAdjacentOlderModel::create()->where(['is_deleted' => 0, 'sid' => $sid])->all();
        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $eid = array_column($examine, 'pid');
        $school = AdjacentOlderModel::create()->where("id", $eid, "IN")->all(['is_deleted' => 0]);
        if (empty($school)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($school as $item) {
            $share = ConfigService::getInstance()->getServiceItem($item['nid']);
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'house' => $item['house'],
                'older_name' => $school['older_name'],
                'older_age' => $school['older_age'],
                'older_sex' => $school['older_sex'],
                'title' => $share->name,
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getShareStatus($item['status']),
                'memo' => $share->label,
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'during' => DataConversion::getInstance()->getDays($item['start_time'], $item['end_time']),
                'fee' => $share->price
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchAdjacentOlder($keyword): array
    {
        $school = AdjacentOlderModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($school)) {
            $school = AdjacentOlderModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($visits)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($school as $item) {
            $share = ConfigService::getInstance()->getServiceItem($item['nid']);
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'house' => $item['house'],
                'older_name' => $school['older_name'],
                'older_age' => $school['older_age'],
                'older_sex' => $school['older_sex'],
                'title' => $share->name,
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getShareStatus($item['status']),
                'memo' => $share->label,
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'during' => DataConversion::getInstance()->getDays($item['start_time'], $item['end_time']),
                'fee' => $share->price
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $type
     * @param $status
     * @param $limit
     * @param $page
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentTools($type, $status, $limit, $page): array
    {
        $where = ['is_deleted' => 0, 'type' => $type, 'status' => $status];
        $model = AdjacentToolModel::create()->where($where);

        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $tools = $model->order('id', 'DESC')->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($tools)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($tools as $item) {

            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'address' => $item['address'],
                'tool' => implode(',', json_decode($item['tools'], true)),
                'title' => '借用工具',
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getToolsStatus($item['status']),
                'time' => date("Y-m-d H:i", $item['start_time']) . ' ~ ' . date('Y-m-d H:i', $item['end_time']),
                'create_time' => date("Y-m-d H:i", $item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function examineAdjacentTools($sid, $id, $status): array
    {
        $tools = AdjacentToolModel::create()->getAdjacent(['id' => $id]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'uid' => $tools->uid,
            'sid' => $sid,
            'pid' => $id,
            'status' => $status == 1 ? 0 : -1,
            'type' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];

        $res = ExamineAdjacentToolsModel::create()->addLog($par);
        if ($res) {
            if ($status == -1) {
                $s = -1;
            } else {
                $s = 2;
            }
            $r = AdjacentToolModel::create()->update(['status' => $s], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $image
     * @param $memo
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function expressAdjacentTools($sid, $id, $image, $memo, $status): array
    {
        $tools = AdjacentToolModel::create()->getAdjacent(['id' => $id, 'status' => 2]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'status' => $status == 1 ? 2 : -1,
            'courier' => $aut->name ?? $aut->nickname,
            'express_image' => $image,
            'express_memo' => $memo,
        ];

        $res = ExamineAdjacentToolsModel::create()->update($par, ['pid' => $id, 'uid' => $tools->uid]);
        if ($res) {
            $r = AdjacentToolModel::create()->update(['status' => $status], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchAdjacentTools($keyword): array
    {
        $school = AdjacentToolModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($school)) {
            $school = AdjacentToolModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($visits)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($school as $item) {
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'address' => $item['address'],
                'tool' => implode(',', json_decode($item['tools'])),
                'title' => '借用工具',
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getToolsStatus($item['status']),
                'time' => date("Y-m-d H:i", $item['start_time']) . ' ~ ' . date('Y-m-d H:i', $item['end_time']),
                'create_time' => date("Y-m-d H:i", $item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function returnAdjacentTools($sid, $id): array
    {
        $tools = AdjacentToolModel::create()->getAdjacent(['id' => $id, 'status' => 1]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }
        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'status' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];
        $res = ExamineAdjacentToolsModel::create()->update($par, ['pid' => $id, 'uid' => $tools->uid]);
        if ($res) {
            $r = AdjacentToolModel::create()->update(['status' => 3], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentToolsDetail($id): array
    {
        $tools = AdjacentToolModel::create()->getAdjacent(['id' => $id]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $tool = $tools->toArray();
        $data = [
            'id' => $tool['id'],
            'nid' => $tool['nid'],
            'name' => $tool['name'],
            'tel' => $tool['tel'],
            'address' => $tool['address'],
            'tool' => implode(',', json_decode($tool['tools'])),
            'title' => '借用工具',
            'type' => $tool['type'],
            'status' => $tool['status'],
            'status_label' => ConfigService::getInstance()->getToolsStatus($tool['status']),
            'time' => date("Y-m-d H:i", $tool['start_time']) . ' ~ ' . date('Y-m-d H:i', $tool['end_time']),
            'create_time' => date("Y-m-d H:i", $tool['create_time'])
        ];

        $examine = ExamineAdjacentToolsModel::create()->getLog(['pid' => $tool['id']]);
        $data['examine'] = $examine ? $examine->toArray() : [];

        return [
            'result' => true,
            'msg' => '查询成功',
            'data' => $data
        ];
    }

    /**
     * @param $status
     * @param $limit
     * @param $page
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentKitchen($status, $limit, $page): array
    {
        $where = ['is_deleted' => 0, 'status' => $status];
        $model = AdjacentKitchenModel::create()->where($where);

        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $tools = $model->order('id', 'DESC')->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($tools)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($tools as $item) {

            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'address' => $item['address'],
                'tool' => implode(',', json_decode($item['tools'])),
                'title' => $item['nid'] == 0 ? '共享场地' : '共享工具',
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getToolsStatus($item['status']),
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'create_time' => date("Y-m-d H:i", $item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function examineAdjacentKitchen($sid, $id, $status): array
    {
        $tools = AdjacentKitchenModel::create()->getAdjacent(['id' => $id]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'uid' => $tools->uid,
            'sid' => $sid,
            'pid' => $id,
            'status' => $status == 1 ? 0 : -1,
            'type' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];

        $res = ExamineAdjacentKitchenModel::create()->addLog($par);
        if ($res) {
            if ($status == -1) {
                $s = -1;
            } else {
                $s = 2;
            }
            $r = AdjacentKitchenModel::create()->update(['status' => $s], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $image
     * @param $memo
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function expressAdjacentKitchen($sid, $id, $image, $memo, $status): array
    {
        $tools = AdjacentKitchenModel::create()->getAdjacent(['id' => $id, 'status' => 2]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'status' => $status == 1 ? 2 : -1,
            'courier' => $aut->name ?? $aut->nickname,
            'express_image' => $image,
            'express_memo' => $memo,
        ];

        $res = ExamineAdjacentKitchenModel::create()->update($par, ['pid' => $id, 'uid' => $tools->uid]);
        if ($res) {
            $r = AdjacentKitchenModel::create()->update(['status' => $status], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchAdjacentKitchen($keyword): array
    {
        $school = AdjacentKitchenModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($school)) {
            $school = AdjacentKitchenModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($visits)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($school as $item) {
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'address' => $item['address'],
                'tool' => implode(',', json_decode($item['tools'])),
                'title' => $item['nid'] == 0 ? '共享场地' : '共享工具',
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getToolsStatus($item['status']),
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'create_time' => date("Y-m-d H:i", $item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function returnAdjacentKitchen($sid, $id): array
    {
        $tools = AdjacentKitchenModel::create()->getAdjacent(['id' => $id, 'status' => 1]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }
        $aut = StaffModel::create()->getUser(['id' => $sid]);

        $par = [
            'status' => 1,
            'author' => $aut->name ?? $aut->nickname
        ];
        $res = ExamineAdjacentKitchenModel::create()->update($par, ['pid' => $id, 'uid' => $tools->uid]);
        if ($res) {
            $r = AdjacentKitchenModel::create()->update(['status' => 3], ['id' => $id]);
        }

        if (!$res || !$r) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAdjacentKitchenDetail($id): array
    {
        $tools = AdjacentKitchenModel::create()->getAdjacent(['id' => $id]);
        if (!$tools) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $tool = $tools->toArray();
        $data = [
            'id' => $tool['id'],
            'nid' => $tool['nid'],
            'name' => $tool['name'],
            'tel' => $tool['tel'],
            'address' => $tool['address'],
            'tool' => implode(',', json_decode($tool['tools'])),
            'title' => $tool['nid'] == 0 ? '共享场地' : '共享工具',
            'status' => $tool['status'],
            'status_label' => ConfigService::getInstance()->getToolsStatus($tool['status']),
            'time' => date("Y-m-d", $tool['start_time']) . ' ~ ' . date('Y-m-d', $tool['end_time']),
            'create_time' => date("Y-m-d H:i", $tool['create_time'])
        ];

        $examine = ExamineAdjacentKitchenModel::create()->getLog(['pid' => $tool['id']]);
        $data['examine'] = $examine ? $examine->toArray() : [];

        return [
            'result' => true,
            'msg' => '查询成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyAdjacentTools($sid): array
    {
        $examine = ExamineAdjacentToolsModel::create()->where(['is_deleted' => 0, 'sid' => $sid])->all();
        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $eid = array_column($examine, 'pid');
        $school = AdjacentToolModel::create()->where("id", $eid, "IN")->all(['is_deleted' => 0]);
        if (empty($school)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($school as $item) {
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'address' => $item['address'],
                'tool' => implode(',', json_decode($item['tools'])),
                'title' => '借用工具',
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getToolsStatus($item['status']),
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'create_time' => date("Y-m-d H:i", $item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyAdjacentKitchen($sid): array
    {
        $examine = ExamineAdjacentKitchenModel::create()->where(['is_deleted' => 0, 'sid' => $sid])->all();
        if (empty($examine)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $eid = array_column($examine, 'pid');
        $school = AdjacentKitchenModel::create()->where("id", $eid, "IN")->all(['is_deleted' => 0]);
        if (empty($school)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($school as $item) {
            $data[] = [
                'id' => $item['id'],
                'nid' => $item['nid'],
                'name' => $item['name'],
                'tel' => $item['tel'],
                'address' => $item['address'],
                'tool' => implode(',', json_decode($item['tools'])),
                'title' => $item['nid'] == 0 ? '共享场地' : '共享工具',
                'status' => $item['status'],
                'status_label' => ConfigService::getInstance()->getToolsStatus($item['status']),
                'time' => date("Y-m-d", $item['start_time']) . ' ~ ' . date('Y-m-d', $item['end_time']),
                'create_time' => date("Y-m-d H:i", $item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getActiveList(): array
    {
        $list = ActiveModel::create()->where(['is_deleted' => 0])->order('id', 'DESC')->all();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }


        $time = time();
        $data = [];
        foreach ($list as $item) {
            $data[] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'free' => $item['type'],
                'price' => $item['price'],
                'join' => $item['join'],
                'limit' => $item['person'],
                'image' => $item['image'],
                'is_hot' => $item['is_hot'],
                'sponsor' => $item['sponsor'],
                'content' => $item['content'],
                'matter' => $item['matter'],
                'address' => $item['address'],
                'contact' => $item['contact'],
                'time' => date("Y-m-d H:i:s", $item['time']),
                'end_time' => date("Y-m-d H:i:s", $item['end_time']),
                'create_time' => date("Y-m-d H:i:s", $item['create_time']),
                'see' => $item['see'],
                'status' => $item['end_time'] < $time ? -1 : 0
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getActiveDetail($sid, $id): array
    {
        $active = ActiveModel::create()->getActive(['id' => $id]);
        if (!$active) {
            return [
                'result' => false,
                'msg' => '该活动已结束'
            ];
        }

        $joins = ActiveJoinModel::create()->where(['gid' => $id, 'status' => 1])->all();
        $users = [];
        if (!empty($joins)) {

            $uids = array_column($joins, 'uid');
            $users = UserModel::create()->where('id', $uids, "IN")->field('id, avatar, username')->all();

            $u = [];
            foreach ($joins as $i) {
                foreach ($users as $user) {
                    if ($i['uid'] == $user['id']) {
                        $u[] = [
                            'status' => $i['status'],
                            'status_label' => $i['status'] == 1 ? '已确认' : '未确认',
                            'complete' => $i['complete'],
                            'complete_label' => $i['complete'] == 1 ? '已参与' : '未参与',
                            'join_time' => date("Y-m-d H:i:s", $i['create_time']),
                            'num' => $i['num'],
                            'name' => $i['name'],
                            'tel' => $i['tel'],
                            'uid' => $i['uid'],
                            'username' => $user['username'],
                            'avatar' => $user['avatar']
                        ];
                    }
                }
            }
        }

        $item = $active->toArray();
        $data = [
            'id' => $item['id'],
            'title' => $item['title'],
            'free' => $item['type'],
            'price' => $item['price'],
            'join' => $item['join'],
            'limit' => $item['person'],
            'is_hot' => $item['is_hot'],
            'sponsor' => $item['sponsor'],
            'content' => $item['content'],
            'matter' => $item['matter'],
            'address' => $item['address'],
            'contact' => $item['contact'],
            'image' => $item['image'],
            'time' => date("Y-m-d H:i:s", $item['time']),
            'end_time' => date("Y-m-d H:i:s", $item['end_time']),
            'create_time' => date("Y-m-d H:i:s", $item['create_time']),
            'see' => $item['see'],
            'users' => $u
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $code
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function completeActive($sid, $id, $code): array
    {
        $join = ActiveJoinModel::create()->getActive(['gid' => $id, 'code' => $code, 'status' => 1, 'complete' => 0]);
        if (!$join) {
            return [
                'result' => false,
                'msg' => '该订单无效'
            ];
        }

        $par = [
            'sid' => $sid,
            'complete' => 1
        ];

        $res = ActiveJoinModel::create()->update($par, ['gid' => $id, 'code' => $code, 'status' => 1]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $type
     * @param $title
     * @param $content
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function releaseNotice($sid, $type, $title, $content): array
    {
        $par = [
            'type' => $type,
            'title' => $title,
            'content' => $content
        ];

        $res = NoticeModel::create()->addChat($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '发布成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $gid
     * @param $name
     * @param $icon
     * @param $image
     * @param $description
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function releaseTopic($sid, $gid, $name, $icon, $image, $description): array
    {
        $par = [
            'name' => $name,
            'icon' => $icon,
            'image' => $image,
            'description' => $description
        ];

        if ($gid == 0) {
            $res = CommunityTopicModel::create()->addConfig($par);
        } else {
            $res = CommunityTopicModel::create()->update($par, ['id' => $gid]);
        }

        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $nid
     * @param $type
     * @param $is_hot
     * @param $price
     * @param $content
     * @param $image
     * @param $title
     * @param $sponsor
     * @param $matter
     * @param $address
     * @param $contact
     * @param $time
     * @param $end_time
     * @param $person
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function releaseActive($sid, $nid, $type, $is_hot, $price, $content, $image, $title, $sponsor, $matter, $address, $contact, $time, $end_time, $person): array
    {
        $par = [
            'uid' => $sid,
            'type' => $type,
            'is_hot' => $is_hot,
            'price' => $price,
            'content' => $content,
            'image' => $image,
            'title' => $title,
            'sponsor' => $sponsor,
            'matter' => $matter,
            'address' => $address,
            'contact' => $contact,
            'time' => strtotime($time),
            'end_time' => strtotime($end_time),
            'person' => $person
        ];

        if ($nid == 0) {
            $res = ActiveModel::create()->addActive($par);
        } else {
            $res = ActiveModel::create()->update($par, ['id' => $nid]);
        }

        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getStaffList(): array
    {
        $staffType = StaffTypeModel::create()->where(['is_deleted' => 0])->all();
        $staff = StaffModel::create()->where(['is_deleted' => 0])->all();
        if (empty($staff) || empty($staffType)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }
        $data = [];
        foreach ($staffType as $type) {
            $par = [
                'tid' => $type['id'],
                'name' => $type['name'],
                'desc' => $type['desc']
            ];
            foreach ($staff as $v) {
                unset($v['password']);
                if ($v['type'] == $type['id']) {
                    $par['data'][] = $v;
                }
            }
            $data[] = $par;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getBadge($sid): array
    {
        $statuses = [-1, 1];

        $visit = ExamineVisitModel::create()->where(['sid' => $sid])->where('status', $statuses, 'NOT IN')->count('id');
        $repair = ExamineRepairModel::create()->where(['sid' => $sid])->where('status', $statuses, 'NOT IN')->count('id');
        $school = ExamineAdjacentSchoolModel::create()->where(['sid' => $sid])->where('status', $statuses, 'NOT IN')->count('id');
        $older = ExamineAdjacentOlderModel::create()->where(['sid' => $sid])->where('status', $statuses, 'NOT IN')->count('id');
        $kitchen = ExamineAdjacentKitchenModel::create()->where(['sid' => $sid])->where('status', $statuses, 'NOT IN')->count('id');
        $tool = ExamineAdjacentToolsModel::create()->where(['sid' => $sid])->where('status', $statuses, 'NOT IN')->count('id');

        $data = [
            'visit' => $visit,
            'repair' => $repair,
            'school' => $school,
            'older' => $older,
            'kitchen' => $kitchen,
            'tool' => $tool
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getReport(): array
    {
        $report = ReportModel::create()->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($report)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $ruid = array_column($report, 'uid');
        $users = UserModel::create()->where("id", $ruid, 'IN')->field('username, phone, id')->all();
        $data = [];
        foreach ($report as $r) {
            foreach ($users as $u) {
                if ($r['uid'] == $u['id']) {
                    $data[] = [
                        'id' => $r['id'],
                        'uid' => $u['id'],
                        'content' => $r['content'],
                        'images' => json_decode($r['images'], true),
                        'username' => $u['username'],
                        'phone' => $u['phone'],
                        'tel' => DataConversion::getInstance()->hidtel($u['phone']),
                        'time' => DataConversion::getInstance()->timeToDate($r['create_time'])
                    ];
                }
            }
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getUserHouse($sid): array
    {
        $houses = UserHouseModel::create()->where("status", "0", "<=")->where(['is_deleted' => 0])->order('id', 'DESC')->all();
        if (empty($houses)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($houses as $key => $house) {
            if ($house['default']) $default = $house['id'];
            $data[] = [
                'id' => $house['id'],
                'house' => ConfigService::getInstance()->getHouse($house['village'], $house['unit'], $house['floor'], $house['house']),
                'name' => $house['name'],
                'userInfo' => ConfigService::getInstance()->getUserInfo($house['uid']),
                'type' => $house['type'],
                'type_value' => ConfigService::getInstance()->houseType($house['type']),
                'default' => $house['default'],
                'status' => $house['status'],
                'time' => date('Y-m-d', $house['create_time']),
                'disabled' => $house['default']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getUserCar($sid): array
    {

        $cars = UserCarModel::create()->where("status", "0", "<=")->where(['is_deleted' => 0])->all();
        if (empty($cars)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($cars as $car) {
            $data[] = [
                'id' => $car['id'],
                'park' => $car['park'],
                'park_value' => ConfigService::getInstance()->getPark($car['park']),
                'image' => $car['image'],
                'name' => $car['name'],
                'tel' => $car['tel'],
                'id_card' => $car['id_card'],
                'number' => $car['number'],
                'type' => $car['type'],
                'type_value' => ConfigService::getInstance()->carType($car['type']),
                'status_label' => ConfigService::getInstance()->getCarStatus($car['status']),
                'brand' => $car['brand'],
                'model' => $car['model'],
                'time' => DataConversion::getInstance()->timeToDate($car['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    public function getUserFamily($sid)
    {

    }

    /**
     * @param $keyword
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchStaff($keyword): array
    {
        $staff = StaffModel::create()->where('phone', '%' . $keyword . '%', 'LIKE')->all();
        if (empty($staff)) {
            $staff = StaffModel::create()->where('name', '%' . $keyword . '%', 'LIKE')->all();
            if (empty($staff)) {
                return [
                    'result' => false,
                    'msg' => '暂无搜索到数据'
                ];
            }
        }

        $staffType = StaffTypeModel::create()->where(['is_deleted' => 0])->all();
        $data = [];
        foreach ($staff as $v) {
            foreach ($staffType as $type) {

                if ($v['type'] == $type['id']) {
                    $par = [
                        'tid' => $type['id'],
                        'name' => $type['name'],
                        'desc' => $type['desc']
                    ];
                    unset($v['password']);
                    $par['data'][] = $v;
                    $data[] = $par;
                }
            }

        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $complete
     * @param $un_complete
     * @param $problem
     * @param $plan
     * @param $info
     * @param $report
     * @param $images
     * @param $file
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function editLog($sid, $complete, $un_complete, $problem, $plan, $info, $report, $images, $file, $id): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '该账户已失效'
            ];
        }
        $par = [
            'sid' => $sid,
            'complete' => $complete,
            'un_complete' => $un_complete,
            'problem' => $problem,
            'plan' => $plan,
            'info' => $info,
            'report' => $report,
            'images' => json_encode($images),
            'file' => $file,
            'author' => $staff->name
        ];
        unset($staff);
        if ($id == 0) {
            $res = LogWorkModel::create()->addLog($par);
        } else {
            $res = LogWorkModel::create()->update($par, ['id' => $id, 'sid' => $sid]);
        }
        if (!$res) {
            return [
                'result' => false,
                'msg' => '提交失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '提交成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $start
     * @param $end
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function selectLog($sid, $start, $end): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '该账户已失效'
            ];
        }

        $type = $staff->type;
        if (!$start) {
            $start = date("Y-m-d");
        }
        if (!$end) {
            $end = date("Y-m-d 23:59:59");
        }
        unset($staff);
        $model = LogWorkModel::create()->where('create_time', strtotime($start), ">")->where('create_time', strtotime($end), '<=');
        $where = ['is_deleted' => 0];
        if ($type > 2) {
            $where['sid'] = $sid;
        }
        $logs = LogWorkModel::create()->where($where)->all();
        if (empty($logs)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }
        $uids = array_column($logs, 'sid');

        $staffs = StaffModel::create()->where("id", $uids, "IN")->all();

        $sta = array_column($staffs, 'avatar', 'id');
        $stn = array_column($staffs, 'name', 'id');
        $data = [];
        foreach ($logs as $log) {
            $log['time'] = DataConversion::getInstance()->timeToDate($log['create_time']);
            $log['img'] = json_decode($log['images'], true);
            $log['avatar'] = $sta[$log['sid']] ?? '';
            $log['author'] = $stn[$log['sid']] ?? '';
            $data[] = $log;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getLogDetail($sid, $id): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '该账户已失效'
            ];
        }

        $type = $staff->type;
        $where = ['id' => $id];
        if ($type > 2) {
            $where['sid'] = $sid;
        }
        unset($staff);
        $log = LogWorkModel::create()->getLog($where);
        if (!$log) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }
        $staff = StaffModel::create()->getUser(['id' => $log->sid]);

        $log = $log->toArray();
        $log['time'] = DataConversion::getInstance()->timeToDate($log['create_time']);
        $log['images'] = json_decode($log['images'], true);
        $log['avatar'] = $staff->avatar ?? '';
        $log['author'] = $staff->name ?? '';
        $data = $log;
        unset($staff);
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $topic
     * @param $content
     * @param $images
     * @param $price
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function createLattice($sid, $topic, $content, $images, $price): array
    {
        $par = [
            'sid' => $sid,
            'topic' => $topic,
            'from' => "来自物业共享格子铺",
            'content' => $content,
            'images' => json_encode($images),
            'price' => $price
        ];

        $res = ShareLatticeGoodsModel::create()->addDynamic($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '发布失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '发布成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $tid
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getLatticeList($tid, $limit, $page): array
    {
        $where = ['is_deleted' => 0];
        $model = ShareLatticeGoodsModel::create();
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        if ($tid != 99) {
            $where['topic'] = $tid;
        }

        $model->order('id', "DESC");
        $list = $model->where($where)->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = StaffModel::create()->getUser(['id' => $item['sid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '物业中心',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getLatticeTopic($item['topic']),
                'content' => $item['content'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time']),
                'price' => $item['price'],
                'status' => $item['status'],
                'status_label' => $item['status'] == 1 ? '已转让' : ''
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getLatticeDetail($id): array
    {
        $lattice = ShareLatticeGoodsModel::create()->getDynamic(['id' => $id]);
        if (!$lattice) {
            return [
                'result' => false,
                'msg' => '该数据不存在'
            ];
        }
        $item = $lattice->toArray();
        $data = [];
        $user = StaffModel::create()->getUser(['id' => $item['sid']]);
        $data['info'] = [
            'id' => $item['id'],
            'avatar' => $user->avatar ?? '',
            'username' => $user->username ?? '物业中心',
            'from' => $item['from'],
            'theme' => ConfigService::getInstance()->getLatticeTopic($item['topic']),
            'content' => $item['content'],
            'images' => json_decode($item['images'], true),
            'time' => DataConversion::getInstance()->timeToDate($item['create_time']),
            'price' => $item['price'],
            'status' => $item['status'],
            'status_label' => $item['status'] == 1 ? '已转让' : ''
        ];
        $order = ShareLatticeOrderModel::create()->where(['gid' => $id]);
        $data['order'] = [];
        if ($order) $data['order'] = $order->toArray();

        return [
            'result' => true,
            'msg' => ' 获取成功',
            'data' => $data
        ];

    }

    /**
     * @param $limit
     * @param $page
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getLatticeOrder($limit, $page, $status): array
    {

        $where = ['is_deleted' => 0];
        if ($status != 99) {
            $where['status'] = $status;
        }
        $model = ShareLatticeOrderModel::create();
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();

        $model->order('id', "DESC");
        $list = $model->where($where)->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无订单'
            ];
        }
        $data = [];
        foreach ($list as $value) {
            $goods = ShareLatticeGoodsModel::create()->getDynamic(['id' => $value['gid']]);
            $goods = $goods->toArray();
            $order = OrderModel::create()->getOrder(['type' => 7, 'rid' => $value['gid']]);
            $user = UserModel::create()->getUser(['id' => $value['uid']]);
            $data[] = [
                'uid' => $user->id,
                'username' => $user->username,
                'avatar' => $user->avatar,
                'from' => $goods['from'],
                'theme' => ConfigService::getInstance()->getLatticeTopic($goods['topic']),
                'content' => $goods['content'],
                'images' => json_decode($goods['images'], true),
                'goods_create_time' => DataConversion::getInstance()->timeToDate($goods['create_time']),
                'price' => $goods['price'],
                'goods_status' => $goods['status'],
                'goods_status_label' => $goods['status'] == 1 ? '已转让' : '',
                'oid' => $order->id ?? 0,
                'name' => $value['name'],
                'address' => $value['address'],
                'tel' => $value['tel'],
                'older_status' => $value['status'],
                'older_create_time' => DataConversion::getInstance()->timeToDate($value['create_time'] ?? ''),
                'id' => $value['id'],
                'order_no' => $order->order_no ?? '',
                'amount_real' => $order->amount_real ?? '',
                'payment_trade' => $order->payment_trade ?? '-',
                'payment_status' => $order->payment_status ?? 0,
                'payment_amount' => $order->payment_amount ?? 0.00,
                'payment_remark' => $order->payment_remark ?? '-',
                'payment_time' => DataConversion::getInstance()->timeToDate($order->payment_time ?? ''),
                'order_status' => $order->status ?? 2,
                'order_create_time' => DataConversion::getInstance()->timeToDate($order->create_time ?? '')
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getLatticeOrderDetail($id): array
    {
        $older = ShareLatticeOrderModel::create()->getAdjacent(['id' => $id]);
        if (!$older) {
            return [
                'result' => false,
                'msg' => '该订单已失效'
            ];
        }

        $value = $older->toArray();
        $goods = ShareLatticeGoodsModel::create()->getDynamic(['id' => $value['gid']]);
        $goods = $goods->toArray();
        $order = OrderModel::create()->getOrder(['type' => 7, 'rid' => $value['gid']]);
        $user = UserModel::create()->getUser(['id' => $value['uid']]);
        $data = [
            'uid' => $user->id,
            'username' => $user->username,
            'avatar' => $user->avatar,
            'from' => $goods['from'],
            'theme' => ConfigService::getInstance()->getLatticeTopic($goods['topic']),
            'content' => $goods['content'],
            'images' => json_decode($goods['images'], true),
            'goods_create_time' => DataConversion::getInstance()->timeToDate($goods['create_time']),
            'price' => $goods['price'],
            'goods_status' => $goods['status'],
            'goods_status_label' => $goods['status'] != 0 ? '已转让/已下架' : '在售',
            'oid' => $order->id ?? 0,
            'name' => $value['name'],
            'address' => $value['address'],
            'tel' => $value['tel'],
            'older_status' => $value['status'],
            'older_create_time' => DataConversion::getInstance()->timeToDate($value['create_time'] ?? ''),
            'id' => $value['id'],
            'order_no' => $order->order_no ?? '',
            'amount_real' => $order->amount_real ?? '',
            'payment_trade' => $order->payment_trade ?? '-',
            'payment_status' => $order->payment_status ?? 0,
            'payment_amount' => $order->payment_amount ?? 0.00,
            'payment_remark' => $order->payment_remark ?? '-',
            'payment_time' => DataConversion::getInstance()->timeToDate($order->payment_time ?? ''),
            'order_status' => $order->status ?? 2,
            'order_create_time' => DataConversion::getInstance()->timeToDate($order->create_time ?? '')
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchLatticeGoods($keyword): array
    {
        $list = ShareLatticeGoodsModel::create()->where("content", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = StaffModel::create()->getUser(['id' => $item['sid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '物业中心',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getLatticeTopic($item['topic']),
                'content' => $item['content'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time']),
                'price' => $item['price'],
                'status' => $item['status'],
                'status_label' => $item['status'] == 1 ? '已转让' : ''
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $keyword
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function searchLatticeOrder($keyword): array
    {
        $list = ShareLatticeOrderModel::create()->where("name", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
        if (empty($list)) {
            $list = ShareLatticeOrderModel::create()->where("tel", '%' . $keyword . '%', "LIKE")->order('id', 'DESC')->all(['is_deleted' => 0]);
            if (empty($list)) {
                return [
                    'result' => false,
                    'msg' => '暂无数据'
                ];
            }
        }

        $data = [];
        foreach ($list as $value) {
            $goods = ShareLatticeGoodsModel::create()->getDynamic(['id' => $value['gid']]);
            $goods = $goods->toArray();
            $order = OrderModel::create()->getOrder(['type' => 7, 'rid' => $value['gid']]);
            $user = UserModel::create()->getUser(['id' => $value['uid']]);
            $data[] = [
                'uid' => $user->id,
                'username' => $user->username,
                'avatar' => $user->avatar,
                'from' => $goods['from'],
                'theme' => ConfigService::getInstance()->getLatticeTopic($goods['topic']),
                'content' => $goods['content'],
                'images' => json_decode($goods['images'], true),
                'goods_create_time' => DataConversion::getInstance()->timeToDate($goods['create_time']),
                'price' => $goods['price'],
                'goods_status' => $goods['status'],
                'goods_status_label' => $goods['status'] == 1 ? '已转让' : '',
                'oid' => $order->id ?? 0,
                'name' => $value['name'],
                'address' => $value['address'],
                'tel' => $value['tel'],
                'older_status' => $value['status'],
                'older_create_time' => DataConversion::getInstance()->timeToDate($value['create_time'] ?? ''),
                'id' => $value['id'],
                'order_no' => $order->order_no,
                'amount_real' => $order->amount_real,
                'payment_trade' => $order->payment_trade ?? '-',
                'payment_status' => $order->payment_status ?? 0,
                'payment_amount' => $order->payment_amount ?? 0.00,
                'payment_remark' => $order->payment_remark ?? '-',
                'payment_time' => $order->payment_time,
                'order_status' => $order->status ?? 2,
                'order_create_time' => $order->create_time ?? ''
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function offShell($id): array
    {
        $res = ShareLatticeGoodsModel::create()->update(['status' => -1], ['id' => $id]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => "系统错误，请稍后重试"
            ];
        }

        return [
            'result' => true,
            'msg' => '下架成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function latticeSend($id): array
    {
        $res = ShareLatticeOrderModel::create()->update(['status' => 4], ['id' => $id]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => "系统错误，请稍后重试"
            ];
        }

        $older = ShareLatticeOrderModel::create()->getAdjacent(['id' => $id]);
        if (!$older) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }

        $oRes = OrderModel::create()->update(['status' => 5], ['rid' => $older->gid, 'uid' => $older->uid, 'type' => 7]);
        if (!$oRes) {
            return [
                'result' => false,
                'msg' => "系统错误，请稍后重试"
            ];
        }

        return [
            'result' => true,
            'msg' => '发货成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function latticeComplete($id): array
    {
        $res = ShareLatticeOrderModel::create()->update(['status' => 1], ['id' => $id]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => "系统错误，请稍后重试"
            ];
        }

        $older = ShareLatticeOrderModel::create()->getAdjacent(['id' => $id]);
        if (!$older) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }

        $oRes = OrderModel::create()->update(['status' => 6], ['rid' => $older->gid, 'uid' => $older->uid, 'type' => 7]);
        if (!$oRes) {
            return [
                'result' => false,
                'msg' => "系统错误，请稍后重试"
            ];
        }

        return [
            'result' => true,
            'msg' => '确认完成',
            'data' => 'success'
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssets(): array
    {
        $assets = AssetsModel::create()->where(['is_deleted' => 0])->all();
        if (empty($assets)) {
            return [
                'result' => false,
                'msg' => '暂无库存'
            ];
        }

        $data = [];

        foreach ($assets as $item) {
            $type = AssetsTypeModel::create()->getConfig(['id' => $item['nid']]);
            $data[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'type' => $type->name,
                'price' => $item['price'],
                'image' => $item['image'],
                'unit' => $item['unit'],
                'label' => $item['label'],
                'stock' => $item['stock'],
                'create_time' => $item['create_time'],
                'update_time' => $item['update_time'],
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssetsType(): array
    {
        $type = AssetsTypeModel::create()->where(['is_deleted' => 0])->all();
        if (empty($type)) {
            return [
                'result' => false,
                'msg' => '暂无分类'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $type
        ];
    }

    /**
     * @param $sid
     * @param $nid
     * @param $name
     * @param $price
     * @param $unit
     * @param $num
     * @param $label
     * @param $time
     * @param $image
     * @param $type
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function assetsIn($sid, $nid, $name, $price, $unit, $num, $label, $time, $image, $type): array
    {
        $par = [
            'sid' => $sid,
            'nid' => $nid,
            'name' => $name,
            'price' => $price,
            'unit' => $unit,
            'num' => $num,
            'label' => $label,
            'type' => $type,
            'time' => $time,
            'image' => $image
        ];

        $res = AssetsInOutModel::create()->addAdjacent($par);

        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败,请重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功,请耐心等待审核',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $num
     * @param $time
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function assetsOut($sid, $id, $num, $time, $type): array
    {
        $assets = AssetsModel::create()->getAdjacent(['id' => $id]);
        if (!$assets || $assets->stock <= $num) {
            return [
                'result' => false,
                'msg' => '暂无该产品'
            ];
        }

        $par = [
            'sid' => $sid,
            'nid' => $assets->nid,
            'rid' => $id,
            'name' => $assets->name,
            'price' => $assets->unit * $num,
            'unit' => $assets->unit,
            'num' => $num,
            'label' => $assets->label,
            'type' => $type,
            'time' => $time,
            'image' => $assets->image
        ];

        $res = AssetsInOutModel::create()->addAdjacent($par);

        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败,请重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功,请耐心等待审核',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function assetsRevert($sid, $id): array
    {
        $log = AssetsInOutModel::create()->getAdjacent(['id' => $id, 'sid' => $sid]);
        if (!$log) {
            return [
                'result' => false,
                'msg' => '操作有误'
            ];
        }

        $res = AssetsInOutModel::create()->update(['revert' => 2], ['id' => $id]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $type
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssetsLogList($type): array
    {
        $where = ['is_deleted' => 0];
        if ($type !== 99) $where['type'] = $type;
        $list = AssetsInOutModel::create()->where($where)->order('id', 'DESC')->all();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = StaffModel::create()->getUser(['id' => $item['sid']]);
            $par = $item;
            $par['nav'] = $this->getAccessType($item['rid']) ?? '';
            $par['avatar'] = $user->avatar ?? '';
            $par['username'] = $user->username ?? '';
            $data[] = $par;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $id
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function assetsExamine($sid, $id, $status): array
    {
        $log = AssetsInOutModel::create()->getAdjacent(['id' => $id]);
        if (!$log || (!$log->is_examine && ($log->type == 1 || $log->type == 2))) {
            return [
                'result' => false,
                'msg' => '该申请暂未审批'
            ];
        }
        $res = AssetsInOutModel::create()->where(['id' => $id])->update(['status' => $status]);
        if (!$res && $log->revert != 2) {
            return [
                'result' => false,
                'msg' => '操作失败，请稍后重试'
            ];
        }

        if ($status < 1) {
            return [
                'result' => true,
                'msg' => '操作成功'
            ];
        }

        $asset = AssetsInOutModel::create()->getAdjacent(['id' => $id]);
        if ($asset->type == 0) {

            $par = [
                'sid' => $sid,
                'nid' => $asset->nid,
                'name' => $asset->name,
                'price' => $asset->price,
                'unit' => $asset->unit,
                'stock' => $asset->num,
                'label' => $asset->label,
                'type' => $asset->type,
                'image' => $asset->image,
                'revert' => 1
            ];

            $res = AssetsModel::create()->addAdjacent($par);
            if ($res) {
                $res = AssetsInOutModel::create()->update(['rid' => $res], ['id' => $id]);
            }
        } elseif ($asset->type == 1) {
            $a = AssetsModel::create()->getAdjacent(['id' => $asset->rid]);
            $num = $a->stock - $asset->num;
            $price = $a->price - $asset->price;
            $res = AssetsModel::create()->where(['id' => $asset->rid])->update(['stock' => $num, 'price' => $price]);
        } elseif ($asset->type == 2) {
            $a = AssetsModel::create()->getAdjacent(['id' => $asset->rid]);
            if ($asset->revert == 2) {
                $num = $a->stock + $asset->num;
                $price = $a->price + $asset->price;
            } elseif ($asset->revert == 0) {
                $num = $a->stock - $asset->num;
                $price = $a->price - $asset->price;
            }
            $res = AssetsModel::create()->where(['id' => $asset->rid])->update(['stock' => $num, 'price' => $price]);
            if ($res && $asset->revert == 2) {
                $res = AssetsInOutModel::create()->update(['revert' => $status], ['id' => $id]);
            }
        }
        if (!$res) {
            return [
                'result' => false,
                'msg' => '审核失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '审核成功',
            'data' => 'success'
        ];
    }

    /**
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssetsData(): array
    {
        $toold = AssetsTypeModel::create()->where(['is_deleted' => 0])->all();
        $data = [];
        foreach ($toold as $tool) {
            $data[] = [
                'name' => $tool['name'],
                'foods' => $this->item_filter($tool['id'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param int $nid
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function item_filter(int $nid = 0): array
    {
        $tags = AssetsModel::create()->all(['nid' => $nid, 'is_deleted' => 0]);
        $data = [];
        if (!$tags) return $data;
        foreach ($tags as $k => $tag) {
            if ($tag['nid'] == $nid)
                $data[$k] = [
                    'name' => $tag['name'],
                    'key' => $tag['name'],
                    'icon' => $tag['icon'],
                    'cat' => $tag['id'],
                    'status' => $tag['stock'] == 0 ? 0 : 1,
                    'stock' => $tag['stock'],
                    'nav' => $this->getAccessType($tag['id']) ?? '未知'
                ];
        }
        return $data;
    }

    /**
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssetsStatic(): array
    {
        $par = [
            'total' => 0,
            'numIn' => 0,
            'numOut' => 0,
            'numInEd' => 0,
            'numOutEd' => 0
        ];
        $par['total'] = AssetsModel::create()->where(['is_deleted' => 0])->sum('stock');
        $par['numIn'] = AssetsInOutModel::create()->where('type', [0, 2], 'IN')->where('revert', 0, '<>')->where(['status' => 0, 'is_deleted' => 0])->sum('num') ?? 0;
        $par['numOut'] = AssetsInOutModel::create()->where('type', [1, 2], 'IN')->where(['status' => 0, 'revert' => 0, 'is_deleted' => 0])->sum('num') ?? 0;
        $par['numInEd'] = AssetsInOutModel::create()->where('type', [0, 2], 'IN')->where(['status' => 1, 'revert' => 1, 'is_deleted' => 0])->sum('num') ?? 0;
        $par['numOutEd'] = AssetsInOutModel::create()->where('type', [1, 2], 'IN')->where(['status' => 1, 'revert' => 0, 'is_deleted' => 0])->sum('num') ?? 0;

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $par
        ];
    }

    /**
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssetsInOutLog($type): array
    {
        $where = ['is_deleted' => 0, 'status' => 1];
        if ($type !== 99) $where['type'] = $type;
        $list = AssetsInOutModel::create()->where($where)->order('id', 'DESC')->all();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = StaffModel::create()->getUser(['id' => $item['sid']]);
            $par = $item;
            $par['nav'] = $this->getAccessType($item['rid']) ?? '未知';
            $par['avatar'] = $user->avatar ?? '';
            $par['username'] = $user->username ?? '';
            $data[] = $par;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sid
     * @param $type
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyAssetsInOutLog($sid, $type, $status): array
    {
        $where = ['is_deleted' => 0, 'sid' => $sid];
        if ($type != 99) $where['type'] = $type;
        if ($status != 99) $where['status'] = $status;

        $list = AssetsInOutModel::create()->where($where)->order('id', 'DESC')->all();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = StaffModel::create()->getUser(['id' => $item['sid']]);
            $par = $item;
            $par['nav'] = $this->getAccessType($item['rid']) ?? '未知';
            $par['avatar'] = $user->avatar ?? '';
            $par['username'] = $user->username ?? '';
            $data[] = $par;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getAssetsReport($type): array
    {
        if ($type == 0) {
            $logs = AssetsInOutModel::create()->where('type', [1, 2], 'IN')->where(['status' => 1, 'revert' => 0])->all();
        } else {
            $logs = AssetsInOutModel::create()->where('type', [0, 2], 'IN')->where(['status' => 1, 'revert' => 1])->all();
        }
        if (empty($logs)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }
        $num = 0;
        $price = 0;
        $list = [];
        foreach ($logs as $item) {
            $k = date("Y-m-d", $item['create_time']);
            $num += $item['num'];
            $price += $item['price'];
            $user = StaffModel::create()->getUser(['id' => $item['sid']]);
            $par = $item;
            $par['nav'] = $this->getAccessType($item['rid']) ?? '';
            $par['avatar'] = $user->avatar ?? '';
            $par['username'] = $user->username ?? '';
            $list[$k][] = $par;
        }

        $data = [
            'total' => [
                'num' => $num,
                'price' => $price
            ],
            'list' => $list
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $rid
     * @return false|mixed|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    private function getAccessType($rid)
    {
        $access = AssetsModel::create()->getAdjacent(['id' => $rid]);

        if (!$access) return false;

        $type = AssetsTypeModel::create()->getConfig(['id' => $access->nid]);

        if (!$type) return false;

        return $type->name;
    }

    /**
     * @param $sid
     * @param $id
     * @param $is_examine
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function approve($sid, $id, $is_examine): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff || $staff->type > 2) {
            return [
                'result' => false,
                'msg' => '当前无权限'
            ];
        }

        $res = AssetsInOutModel::create()->update(['is_examine' => $is_examine], ['id' => $id]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function storehouseStatic(): array
    {
        $assets = AssetsModel::create()->where(['is_deleted' => 0])->all();
        if (empty($assets)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($assets as $v) {
            $par = $v;
            $par['nav'] = $this->getAccessType($v['nid']) ?? '未知';
            $data[$v['nav']]['static']['num'] = 0;
            $data[$v['nav']]['static']['price'] = 0;
            $data[$v['nav']]['nav'] = $par;
            $data[$v['nav']]['list'][] = $par;
        }

        $count = 0;
        $sumPrice = 0;
        foreach ($data as $k => $v) {
            foreach ($v['list'] as $i) {
                $count += $i['stock'];
                $sumPrice += $i['price'];
                $data[$k]['static']['num'] += $i['stock'];
                $data[$k]['static']['price'] += $i['price'];
            }
        }

        $data = array_values($data);
        $result = [
            'static' => [
                'num' => $count,
                'price' => $sumPrice
            ],
            'list' => $data
        ];
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $result
        ];
    }

    /**
     * @param $sid
     * @param $content
     * @param $images
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function repair($sid, $content, $images, $type): array
    {
        $staff = StaffModel::create()->getUser(['id' => $sid]);
        if (!$staff) {
            return [
                'result' => false,
                'msg' => '您已被注销'
            ];
        }

        $par = [
            'sid' => $sid,
            'time' => time(),
            'name' => $staff->nickname,
            'tel' => $staff->phone,
            'type' => $type,
            'content' => $content,
            'images' => json_encode($images)
        ];

        $res = PropertyRepairModel::create()->addProperty($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }
        $time = DataConversion::getInstance()->timeToDate(time());
        $str = $time . "有一条新的报事报修订单消息，请及时查看";
        $icon = "https://resourse.cnlhjt.com/upload/20230410/2597323f5d2270449b50de170ece8f28.jpg";
        $title = '报事报修消息';
        $type = 2;
        $nav = 2;
        $staffType = StaffTypeModel::create()->where("rule", "%," . $nav . ",%", "LIKE")->all();
        $tids = array_column($staffType, 'id');
        $staff = StaffModel::create()->where("type", $tids, "IN")->all();
        $sids = array_column($staff, 'id');
        NoticeService::getInstance()->sendNotice($sids, $title, $icon, $str, $type);

        return [
            'result' => true,
            'msg' => '提交成功',
            'data' => 'success'
        ];
    }

}